/*
 * File: Fu_MoCoord.c
 *
 * Code generated for Simulink model 'Fu_MoCoord'.
 *
 * Model version                  : 1.557
 * Simulink Coder version         : 8.2 (R2012a) 29-Dec-2011
 * TLC version                    : 8.2 (Dec 29 2011)
 * C/C++ source code generated on : Sun Jun 16 13:53:03 2013
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: ARM Compatible->ARM Cortex
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Fu_MoCoord.h"
#include "Fu_MoCoord_private.h"

/* Exported block signals */
int16_T Emi_CurQReq;                   /* '<S1>/Switch1' */
int16_T Emi_CurrQReqFF;                /* '<S1>/1-D Lookup Table' */

/* Block states (auto storage) */
D_Work_Fu_MoCoord Fu_MoCoord_DWork;

/* Real-time model */
RT_MODEL_Fu_MoCoord Fu_MoCoord_M_;
RT_MODEL_Fu_MoCoord *const Fu_MoCoord_M = &Fu_MoCoord_M_;

/* Model step function */
void Fu_MoCoord_step(void)
{
  int32_T tmp;
  uint16_T rtb_RateLimiter;
  int16_T rtb_Subtract;

  /* Switch: '<S1>/Switch' incorporates:
   *  Constant: '<S1>/Constant'
   *  Constant: '<S1>/Constant1'
   *  Inport: '<Root>/In1'
   */
  if (Coord_States >= 2) {
    rtb_RateLimiter = 3000U;
  } else {
    rtb_RateLimiter = 1000U;
  }

  /* End of Switch: '<S1>/Switch' */

  /* RateLimiter: '<S1>/Rate Limiter' */
  if (rtb_RateLimiter - Fu_MoCoord_DWork.PrevY > 5) {
    rtb_RateLimiter = (uint16_T)(Fu_MoCoord_DWork.PrevY + 5);
  } else {
    if (rtb_RateLimiter - Fu_MoCoord_DWork.PrevY < -5) {
      rtb_RateLimiter = (uint16_T)(Fu_MoCoord_DWork.PrevY - 5);
    }
  }

  Fu_MoCoord_DWork.PrevY = rtb_RateLimiter;

  /* End of RateLimiter: '<S1>/Rate Limiter' */

  /* Sum: '<S1>/Subtract' incorporates:
   *  Inport: '<Root>/In3'
   */
  rtb_Subtract = (int16_T)((int16_T)rtb_RateLimiter - RotSpd_HallSnsrRPM);

  /* Saturate: '<S1>/Saturation' */
  tmp = rtb_Subtract << 5;
  if (tmp >= 32000) {
    rtb_Subtract = 32000;
  } else if (tmp <= 0) {
    rtb_Subtract = 0;
  } else {
    rtb_Subtract <<= 5;
  }

  /* End of Saturate: '<S1>/Saturation' */

  /* Switch: '<S1>/Switch1' incorporates:
   *  Constant: '<S1>/Constant2'
   *  Inport: '<Root>/In1'
   *  Product: '<S1>/Product'
   */
  if (Coord_States >= 2) {
    /* Product: '<S1>/Product' */
    tmp = rtb_Subtract * 25;
    tmp = (((tmp < 0 ? 255 : 0) + tmp) >> 8) * 5243;
    Emi_CurQReq = (int16_T)(((tmp < 0 ? 4095 : 0) + tmp) >> 12);
  } else {
    Emi_CurQReq = 0;
  }

  /* End of Switch: '<S1>/Switch1' */

  /* Lookup_n-D: '<S1>/1-D Lookup Table' */
  Emi_CurrQReqFF = look1_1fbcpvw2bsal(rtb_RateLimiter,
    Fu_MoCoord_ConstP.DLookupTable_bp01Data,
    Fu_MoCoord_ConstP.DLookupTable_tableDat, &Fu_MoCoord_DWork.m_bpIndex, 5U);

  /* Update for DiscreteIntegrator: '<S1>/Discrete-Time Integrator' */
  Fu_MoCoord_DWork.DiscreteTimeIntegrator_DSTATE += 5243 * rtb_Subtract >> 18;
  if (Fu_MoCoord_DWork.DiscreteTimeIntegrator_DSTATE >= 640) {
    Fu_MoCoord_DWork.DiscreteTimeIntegrator_DSTATE = 640;
  } else {
    if (Fu_MoCoord_DWork.DiscreteTimeIntegrator_DSTATE <= -640) {
      Fu_MoCoord_DWork.DiscreteTimeIntegrator_DSTATE = -640;
    }
  }

  /* End of Update for DiscreteIntegrator: '<S1>/Discrete-Time Integrator' */

  /* Update absolute time for base rate */
  /* The "clockTick0" counts the number of times the code of this task has
   * been executed. The resolution of this integer timer is 0.02, which is the step size
   * of the task. Size of "clockTick0" ensures timer will not overflow during the
   * application lifespan selected.
   */
  Fu_MoCoord_M->Timing.clockTick0++;
}

/* Model initialize function */
void Fu_MoCoord_initialize(void)
{
  /* Registration code */

  /* initialize real-time model */
  (void) memset((void *)Fu_MoCoord_M, 0,
                sizeof(RT_MODEL_Fu_MoCoord));

  /* block I/O */

  /* exported global signals */
  Emi_CurQReq = 0;
  Emi_CurrQReqFF = 0;

  /* states (dwork) */
  (void) memset((void *)&Fu_MoCoord_DWork, 0,
                sizeof(D_Work_Fu_MoCoord));

  /* InitializeConditions for RateLimiter: '<S1>/Rate Limiter' */
  Fu_MoCoord_DWork.PrevY = 0U;
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
